class calcor:
    def DDsharpe(self,a,b,c):
        if a<b>c:
            return 1
        elif a>b<c:
            return -1
        else:
            return 0
    def beili(self,a,b):
        if a[1]>a[0] and b[0]>b[1]:
            return 1
        elif a[1]<a[0] and b[1]>b[0]:
            return -1
        else:
            0
    def cross(self,a,b):
        if a[0]<b[0] and a[1]>b[1]:
            return 1
        elif a[0]>b[0] and a[1]<b[1]:
            return -1
        else:
            0
    def crossline(self,a,b,l=0):
        if a<l<b:
            return 1
        elif a>l>b:
            return -1
        else:
            return 0
class basebar:
    def __init__(self,fx="up"):
        self.dddata=[]
        self.ddkdata=None
        self.ddkfx=fx
        self.crossdata=[]
        self.crosslinedata=None
        self.clacor=calcor()
    def DDsharpe_onbar(self,bar,filed):
        c=bar[filed]
        rst=0
        if len(self.dddata)>=2:
            rst= self.clacor.DDsharpe(self.dddata[-2],self.dddata[-1],c)
            self.dddata.pop(0)
            self.dddata.append(c)
        else:
            self.dddata.append(c)
        return rst
    def cross_onbar(self,bar,fileda,filedb):
        a=bar[fileda]
        b=bar[filedb]
        rst=0
        if self.crossdata:
            rst=self.clacor.cross((self.crossdata[0],a),(self.crossdata[1],b))
            self.crossdata=[a,b]
        else:
            self.crossdata = [a, b]
        return rst
    def cross_line(self,bar,fileda,line=0):
        a = bar[fileda]
        rst=0
        if self.crosslinedata:
            rst=self.clacor.crossline(self.crosslinedata,a,line)
            self.crosslinedata=a
        else:
            self.crosslinedata = a
        return rst
    def beili(self,a,b):
        return self.clacor.beili(a,b)
    def DDKsharpe_onbar(self,bar):
        rst = 0
        rstck=self.ddkdata
        if self.ddkdata:
            h=self.ddkdata["high"]
            l = self.ddkdata["low"]
            if self.ddkfx=="up":
                rst=0.5
                if h>bar["high"] and l>bar["low"]:
                    self.ddkfx = "down"
                    rst = 1
                    self.ddkdata = self.bar2ck(bar)
                elif h<bar["high"] and l<bar["low"]:
                    self.ddkdata = self.bar2ck(bar)
                else:
                    if bar["high"]>=h:
                        self.ddkdata["high"]=bar["high"]
                        if "hightime" in bar.keys():
                            self.ddkdata["hightime"] = bar["hightime"]
                        else:
                            self.ddkdata["hightime"] = bar["timekey"]
                    if bar["low"]>=l:
                        self.ddkdata["low"] = bar["low"]
                        if "lowtime" in bar.keys():
                            self.ddkdata["lowtime"] = bar["lowtime"]
                        else:
                            self.ddkdata["lowtime"] = bar["timekey"]
            else:
                rst=-0.5
                if h<bar["high"] and l<bar["low"]:
                    self.ddkfx = "up"
                    rst=-1
                    self.ddkdata = self.bar2ck(bar)
                elif h>bar["high"] and l>bar["low"]:
                    self.ddkdata = self.bar2ck(bar)
                else:
                    if bar["high"]<=h:
                        self.ddkdata["high"]=bar["high"]
                        if "hightime" in bar.keys():
                            self.ddkdata["hightime"] = bar["hightime"]
                        else:
                            self.ddkdata["hightime"] = bar["timekey"]
                    if bar["low"]<=l:
                        self.ddkdata["low"] = bar["low"]
                        if "lowtime" in bar.keys():
                            self.ddkdata["lowtime"] = bar["lowtime"]
                        else:
                            self.ddkdata["lowtime"] = bar["timekey"]
        else:
            self.ddkdata=self.bar2ck(bar)
            rstck=self.ddkdata
        return rst,rstck
    def bar2ck(self,bar):
            ck={}
            ck["high"]=bar["high"]
            ck["low"] = bar["low"]
            ck["open"] = bar["open"]
            ck["close"] = bar["close"]
            ck["timekey"]=bar["timekey"]
            if "hightime" in bar.keys():
                ck["hightime"] = bar["hightime"]
            else:
                ck["hightime"] = bar["timekey"]
            if "lowtime" in bar.keys():
                ck["lowtime"] = bar["lowtime"]
            else:
                ck["lowtime"] = bar["timekey"]
            return ck
            